package com.jiyici;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;

import com.actionbarsherlock.app.SherlockFragment;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.MenuItem;
import com.actionbarsherlock.view.Window;
import com.jiyici.controller.DataManager;
import com.jiyici.controller.DataManager.DataListener;
import com.jiyici.fragment.ItemInfoFragment;
import com.jiyici.fragment.RecordCalendarFragment;
import com.jiyici.fragment.RecordListFragment;
import com.jiyici.model.Item;
import com.jiyici.utils.LogUtil;

public class ItemActivity extends SherlockFragmentActivity implements ItemHolder {

    private static final String TAG = "item-activity";

    private Item mItem;
    String[] mViewModeArray;
    private int mIndex = -1;
    
    private BroadcastReceiver mDataChangeBroadcastReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (DataManager.ACTION_DATA_CHANGED.equals(action)) {
                int dataType = intent.getIntExtra(DataManager.EXTRA_KEY_DATA_TYPE, 0);
                if (dataType == DataManager.DATA_CHANGED_RECORD) {
                    DataListener listener = (DataListener) getSupportFragmentManager().findFragmentById(
                            R.id.item_view_container1);
                    listener.onRecordChanged();
                    listener = (DataListener) getSupportFragmentManager().findFragmentById(
                            R.id.item_view_container2);
                    listener.onRecordChanged();
                } else if (dataType == DataManager.DATA_CHANGED_CATEGORY_ITEM) {
                    mItem = DataManager.getItem(mItem.getId());
                    DataListener listener = (DataListener) getSupportFragmentManager().findFragmentById(
                            R.id.item_view_container1);
                    listener.onItemCatgoryChanged();
                    listener = (DataListener) getSupportFragmentManager().findFragmentById(
                            R.id.item_view_container2);
                    listener.onItemCatgoryChanged();
                }
            }
        }

    };
    
    public Item getItem() {
        return mItem;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_ACTION_BAR_OVERLAY);
        GlobalTheme.applyTheme(this, false);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        mItem = (Item) getIntent().getSerializableExtra("item");
        getSupportActionBar().setTitle(mItem.itemName);

        // /* prepare list navigation */
        // mViewModeArray =
        // getResources().getStringArray(R.array.record_view_mode);
        // Context context = getSupportActionBar().getThemedContext();
        // ArrayAdapter<CharSequence> list =
        // ArrayAdapter.createFromResource(context, R.array.record_view_mode,
        // R.layout.sherlock_spinner_item);
        // list.setDropDownViewResource(R.layout.sherlock_spinner_dropdown_item);
        //
        // getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
        // getSupportActionBar().setListNavigationCallbacks(list, this);

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_item);

        if (savedInstanceState == null)
            switch2ViewMode(0);
        
        IntentFilter filter = new IntentFilter();
        filter.addAction(DataManager.ACTION_DATA_CHANGED);
        filter.addAction(DataManager.ACTION_LOAD_DATA_FAILED);
        filter.addAction(DataManager.ACTION_START_LOAD_DATA);
        registerReceiver(mDataChangeBroadcastReceiver, filter);
    }

    @Override
    protected void onDestroy() {
        unregisterReceiver(mDataChangeBroadcastReceiver);
        super.onDestroy();
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (android.R.id.home == item.getItemId()) {
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    private void switch2ViewMode(int index) {
        LogUtil.info(TAG, "switch2ViewMode: " + index + ", current : " + mIndex);
        if (index == mIndex)
            return;
        SherlockFragment fragment = null;
        if (index == 0) {
            FragmentTransaction transaction = null;
            transaction = getSupportFragmentManager().beginTransaction();
            // fragment =
            // (SherlockFragment)getSupportFragmentManager().findFragmentById(R.id.item_view_container);
            fragment = new ItemInfoFragment();
            fragment.setHasOptionsMenu(true);
            transaction.add(R.id.item_view_container1, fragment);
            transaction.commit();

            transaction = getSupportFragmentManager().beginTransaction();
            // fragment =
            // (SherlockFragment)getSupportFragmentManager().findFragmentById(R.id.item_view_container);
            fragment = new RecordListFragment();
            transaction.add(R.id.item_view_container2, fragment);
            transaction.commit();
        }

        mIndex = index;
    }
    
    public void showTable() {
        FragmentTransaction transaction = null;
        transaction = getSupportFragmentManager().beginTransaction();
        SherlockFragment fragment = new RecordListFragment();
        transaction.replace(R.id.item_view_container2, fragment);
        transaction.commit();
    }
    
    public void showCalendar() {
        FragmentTransaction transaction = null;
        transaction = getSupportFragmentManager().beginTransaction();
        SherlockFragment fragment = new RecordCalendarFragment();
        transaction.replace(R.id.item_view_container2, fragment);
        transaction.commit();
    }

    // @Override
    // public boolean onNavigationItemSelected(int itemPosition, long itemId) {
    // switch2ViewMode(itemPosition);
    // return true;
    // }

}
